import Validate from "../../../utils/validate";
import util, {formatDate} from "../../../utils/util";
import network from "../../../utils/network";
import Toast from '@vant/weapp/toast/toast';

const app = getApp()
Component({
    properties: {
        customer_id: {
            type: Number,
            value: -1
        }
    },
    data: {
        date: '',
        calendarShow: false,
        calendarMaxDate: new Date().getTime(),
        validateFollows: {},
        follow: {
            follow_time: null,
            opposite_person: null,// 联系人
            phone: null,
            action_list: [],
            resource_list: [],
            url_list: [],
            reasons_for_recommendation: null,// 拜访目的
            achievements: null,// 达成成果
        },
        resourceArr: [],
        actionArr: [],
        linkUserShow: false,
        linkUserArr: [],
        linkUserLoading: false,
        beforeClose: (e) => {
            return e === 'cancel'
        }
    },
    ready() {
        this.initValidateFollows()
        Promise.all([
            this.getDictList('khxg-tjzy', 'resourceArr', '推荐资源数据初始化失败'),
            this.getDictList('khxg-tjdz', 'actionArr', '推进动作数据初始化失败'),
        ]).then(() => {
            util.hideLoading()
        }).catch(() => {
            util.hideLoading()
        })
    },
    methods: {
        preventTouchMove() {

        },
        onClose() {
            let tmp = {
                follow_time: null,
                opposite_person: null,// 联系人
                phone: null,
                action_list: [],
                resource_list: [],
                url_list: [],
                reasons_for_recommendation: null,// 拜访目的
                achievements: null,// 达成成果
            }
            this.setData({
                follow: tmp
            })
        },
        initValidateFollows() {
            const rules = {
                follow_time: {
                    required: true,
                },
                opposite_person: {
                    required: true
                },
                reasons_for_recommendation: {
                    required: true
                },
                achievements: {
                    required: true
                }
            }
            const messages = {
                follow_time: {
                    required: '请选择沟通日期'
                },
                opposite_person: {
                    required: '请选择对接人'
                },
                reasons_for_recommendation: {
                    required: '请输入拜访目的'
                },
                achievements: {
                    required: '请输入达成成果'
                }
            }
            this.data.validateFollows = new Validate(rules, messages)
        },
        // 多选框选择事件
        onCheckBoxChange(e) {
            const key = e.target.dataset.key;
            this.setData({
                [key]: e.detail,
            });
        },
        // 输入框输change
        onInputChange(e) {
            const key = e.target.dataset.key;
            this.setData({
                [key]: e.detail
            })
        },
        showLinkUserPop() {
            this.setData({
                linkUserShow: true,
                linkUserLoading: true,
            })
            const that = this
            network.reqPromise('GET', {
                uri: app.api.getContactById,
                params: {id: this.data.customer_id}
            }).then(res => {
                that.setData({
                    linkUserArr: res.data,
                    linkUserLoading: false
                })
            }).catch(() => {
                that.setData({
                    linkUserLoading: false
                })
                Toast.fail({
                    message: '初始化联系人失败',
                    context: that,
                    selector: '#custom-selector'
                });
            })
        },
        uploadFile(e) {
            util.showLoading("上传中")
            const that = this
            network.uploadFile(e.detail.file).then(res => {
                let key = 'follow.url_list'
                that.data.follow.url_list.push({
                    url: res.data,
                    type: 'image',
                    isImage: true
                })
                that.setData({
                    [key]: that.data.follow.url_list
                })
                util.hideLoading()
            }).catch((err) => {
                Toast.fail({
                    message: '上传失败',
                    context: this,
                    selector: '#custom-selector'
                })
                util.hideLoading()
            })
        },
        //初始化字典数据
        getDictList(code, data, failMsg) {
            const that = this
            return new Promise((resolve, reject) => {
                network.reqPromise('GET', {
                    uri: app.api.getDictData,
                    params: {code: code}
                }).then(res => {
                    that.setData({
                        [data]: res.data
                    })
                    resolve()
                }).catch(() => {
                    Toast.fail({
                        message: failMsg,
                        context: that,
                        selector: '#custom-selector'
                    });
                    reject()
                })
            })
        },
        showDialog() {
            this.setData({
                show: true
            })
        },
        showCalendar() {
            this.setData({
                calendarShow: true
            })
        },
        onCalendarClose() {
            this.setData({
                calendarShow: false
            })
        },
        onCalendarConfirm(e) {
            let key = 'follow.follow_time'
            this.setData({
                [key]: formatDate(e.detail),
                calendarShow: false,
            });
        },
        onConfirmLinkUser(e) {
            let key = 'follow.opposite_person'
            this.setData({
                [key]: e.detail.value,
                linkUserShow: false
            })
        },
        onCancelPop() {
            this.setData({
                linkUserShow: false
            })
        },
        dialogConfirm() {
            if (!this.data.validateFollows.checkForm(this.data.follow)) {
                Toast.fail({
                    message: this.data.validateFollows.errorList[0].msg,
                    context: this,
                    selector: '#custom-selector'
                })
            } else {
                // if (!this.data.follow.url_list.length) {
                //     Toast.fail({
                //         message: '请上传图片',
                //         context: this,
                //         selector: '#custom-selector'
                //     })
                // } else {
                //     const data = this.data.follow
                //     data.url_list = data.url_list.map(item => {
                //         return item.url
                //     })
                //     data.id = this.data.customer_id
                //     util.showLoading('提交中')
                //     const that = this
                //     network.reqPromise('POST', {
                //         uri: app.api.addVisitRecord,
                //         params: data
                //     }).then(res => {
                //         that.setData({
                //             show: false
                //         })
                //         that.onClose()
                //         util.hideLoading()
                //         that.triggerEvent('success')
                //     }).catch(() => {
                //         util.hideLoading()
                //     })
                // }
                let data = this.data.follow;
                data.url_list = data.url_list.map(item => {
                  return item.url
                });
                data.id = this.data.customer_id;
                util.showLoading('提交中');
                const that = this;
                network.reqPromise('POST', {
                  uri: app.api.addVisitRecord,
                  params: data
                }).then(res => {
                  that.setData({
                      show: false
                  })
                  that.onClose()
                  util.hideLoading()
                  that.triggerEvent('success')
                }).catch(() => {
                  util.hideLoading()
                });
            }
        },
        removeImg(e) {
            let key = 'follow.url_list'
            this.data.follow.url_list.splice(e.detail.index, 1);
            this.setData({
                [key]: this.data.follow.url_list
            })
        }
    }
});
